﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;

using InfFront.UICore;
using InfFront.InfoSources.Base;
using InfFront.InfoViews.Base;

namespace InfFront.InfoViews.WPF.Settings
{
    internal partial class InfoSourceSettingsInfoSource : BaseSettingsInfoSource
    {
        #region Private Variables
        private InfoViewSource viewSource = null;
        #endregion Private Variables

        public InfoSourceSettingsInfoSource()
        {
            InitializeComponent();
        }

        public InfoSourceSettingsInfoSource(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        [Browsable(false), DefaultValue(null)]
        public InfoViewSource ViewSource
        {
            get { return viewSource; }
            set 
            { 
                viewSource = value;
                if (value != null)
                {
                    Title = value.Title;
                    Name = TranslateInfoViewNameToSettingsName(value.Name);
                }
            }
        }

        internal static string TranslateInfoViewNameToSettingsName(string name)
        {
            return "__page__" + name;
        }

        private void InfoSourceSettingsInfoSource_OnGetObject(object sender, GetObjectEventArgs args)
        {
            args.Object = ViewSource;
        }

        private void this_BeforeGetNestedSources(object sender, EventArgs e)
        {
            if (NestedSources.Count <= 2) // only fields and commands are defined
            {
                foreach (var vsrc in ViewSource.NestedSources)
                {
                    var src = new InfoSourceSettingsInfoSource() { ViewSource = vsrc };
                    NestedSources.Add(src);
                }
            }
            if (fieldsDummyInfoSource.NestedSources.Count == 0)
            {
                foreach (var vfld in ViewSource.Fields)
                {
                    var src = new InfoFieldSettingsInfoSource() { ViewField = vfld };
                    fieldsDummyInfoSource.NestedSources.Add(src);
                }
                if (fieldsDummyInfoSource.NestedSources.Count == 0)
                    fieldsDummyInfoSource.IsEnabled = false;
            }
            if (commandsDummyInfoSource.NestedSources.Count == 0)
            {
                foreach (var vcmd in ViewSource.Commands)
                {
                    var src = new InfoCommandSettingsInfoSource() { ViewCommand = vcmd };
                    commandsDummyInfoSource.NestedSources.Add(src);
                }
                if (commandsDummyInfoSource.NestedSources.Count == 0)
                    commandsDummyInfoSource.IsEnabled = false;
            }
        }

        private void fDescription_OnGetValue(object sender, GetIntegralFieldValueEventArgs args)
        {
            var raws = (RawString)ViewSource.VisualSettings.GetValue(BaseSettingsManager.FORMATTED_PAGE_DESCRIPTION_KEY);
            args.Value = raws.Value;
        }

        private void fDescription_OnSetValue(object sender, SetIntegralFieldValueEventArgs args)
        {
            var raws = new RawString() { Value = (string)args.Value };
            ViewSource.VisualSettings.SetValue(BaseSettingsManager.FORMATTED_PAGE_DESCRIPTION_KEY, raws);
        }
    }
}
